<template>
  <el-card shadow="never" class="border-0">
    <!-- 新增|刷新 -->
    <AddHeader @handleCreate="handleCreate" @getData="getData" />

    <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
      <el-table-column prop="title" label="公告标题" />
      <el-table-column prop="create_time" label="发布时间" width="380" />
      <el-table-column label="操作" width="180" align="center">
        <template #default="scope">
          <el-button
            type="primary"
            size="small"
            text
            @click="handleCreate(scope.row)"
            >修改</el-button
          >

          <el-popconfirm
            title="是否要删除该公告？"
            confirmButtonText="确认"
            cancelButtonText="取消"
            @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button text type="primary" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="flex items-center justify-center mt-5">
      <el-pagination
        background
        layout="prev, pager ,next"
        :total="total"
        :current-page="currentPage"
        @current-change="getData"
      />
    </div>
  </el-card>

  <FormDrawer :title="title" ref="popRef" @submit="onSubmit">
    <el-form :model="form" ref="formRef" :rules="rules">
      <el-form-item label="title" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="content" prop="content">
        <el-input v-model="form.content" type="textarea" :rows="5" />
      </el-form-item>
    </el-form>
  </FormDrawer>
</template>
<script setup>
import { reactive, ref, computed } from 'vue'
import {
  getNoticeList,
  deleteNotice,
  addNotice,
  updateNotice,
} from '@/api/http'
import { toast } from '@/composables/util'
import FormDrawer from '@/components/FormDrawer.vue'
import AddHeader from '@/components/AddHeader.vue'
const tableData = ref([])
const loading = ref(false)

const title = computed(() => (editId.value ? '修改' : '新增'))
// 分页
const currentPage = ref(1)
const total = ref(0)

// 获取数据
function getData(p = null) {
  if (typeof p == 'number') {
    currentPage.value = p
  }

  loading.value = true
  getNoticeList(currentPage.value)
    .then((res) => {
      tableData.value = res.data.list
      total.value = res.data.totalCount
    })
    .finally(() => {
      loading.value = false
    })
}

getData()

// 删除
const handleDelete = (id) => {
  deleteNotice(id).then((res) => {
    getData()
    toast('删除成功！')
  })
}

// 表单
const editId = ref(0)
const formRef = ref(null)
const popRef = ref(null)
const form = reactive({
  title: '',
  content: '',
})

const rules = reactive({
  title: {
    required: true,
  },
  content: {
    required: true,
  },
})

const onSubmit = () => {
  formRef.value.validate((valid, fields) => {
    if (valid) {
      const fn = editId.value
        ? updateNotice(editId.value, form)
        : addNotice(form)
      fn.then((res) => {
        getData()
        toast(editId.value ? '修改成功！' : '添加成功！')
      }).finally(() => {
        popRef.value.showDrawer(false)
      })
    }
  })
}

// 重置
const resetForm = (row = false) => {
  if (formRef.value) formRef.value.resetFields()
  if (row) {
    for (const key in form) {
      form[key] = row[key]
    }
  }
}
//新增 - 修改
const handleCreate = (row = 0) => {
  if (row) {
    editId.value = row.id
    resetForm(row)
  } else {
    resetForm()
  }

  popRef.value.showDrawer(true)
}
</script>
